const express=require('express')
const router=express.Router();
const db=require('../model/user')


router.post('/', async (req,res)=>{

  console.log(req.body)
  var data={status:0,msg:null,needCap:false}
  var needCap=req.session.try>5;
  
  var verify=[];
  if(needCap){
    if(req.body.captcha.length!=0){
      verify=await db.User.find({account:req.body.account,password:req.body.password})
    }
    data.needCap=true
  }else{
    verify=await db.User.find({account:req.body.account,password:req.body.password})
  }

  if(verify.length==0){
    req.session.try++;
    data.msg='登录失败请检查用户名或密码是否正确'
    res.send(data)
  }
  if(verify.length>0){
    req.session.try=0;
    req.session.login=true;
    data.msg='success';
    data.status=1;
    res.cookie('login','true',{maxAge:1000*60*60*24*30})
    res.send(data)
  }
  console.log(req.session)
  


})


module.exports=router